{% extends "group_base.html"%}
{% load commonFilters %}
{% load i18n %}

{% comment %}
copyright (c) 2009, paketka@gmail.com et. al
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

	* Redistributions of source code must retain the above copyright notice,
	  this list of conditions and the following disclaimer.

	* Redistributions in binary form must reproduce the above copyright notice,
	  this list of conditions and the following disclaimer in the documentation
	  and/or other materials provided with the distribution.

	* Neither the name of the <ORGANIZATION> nor the names of its contributors
	  may be used to endorse or promote products derived from this software
 	  without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
{% endcomment %}

{% comment %}
template, which renders html page with day/week/month overview for given MP
{% endcomment %}

{% block keywords %}
poslanec, {{ mp.name }} {{ mp.surname }}, poslankyně, zvolené období, detail, {{ mp.term.getStart }} - {{ mp.term.getEnd }},
funkční období, jak jsem hlasoval
{% endblock %}
{% block css-links %}
	<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}/css/ui.jqgrid.css" />
{% endblock %}

{% block js-links %}
	{{ block.super }}
	{% ifequal tabIndex 0 %}
	{% endifequal %}
{% endblock %}

{% block javascript-head %}

{% comment %}
	accordCharts is a javascript array of records.
	each record looks as follows:
		{	url: 'an ajax url where from chart should be fetched',
			optName: 'Option name for selection list'
			imgCache: 'store img element here once it's been fetched by ajax
			groupId: group.id
		}
{% endcomment %}
var accordCharts = [
			{
				groupTypeOpt: '{% trans "Vyber druh skupiny" %}',
				groups: []
			}
		{% for gt in groupTypes %}
			{% if forloop.first %}
			,
			{% endif %}
			{
				groupTypeOpt: '{{ gt.type|truncatewords:4|title }}',
				groups: [
							{
								url: '',
								optName: '{% trans "Vyber skupinu" %}',
								imgCache : '',
								groupId : '',
								groupSlug : ''
							}
					{% for g in gt.groups %}
						{% if forloop.first %}
							,
						{% endif %}
							{
								{% if rangeType %}
								url : '{% url json.views.get_group_accordance group.id, g.id, rangeType, date.year, date.month, date.day %}',
								{% else %}
								url : '{% url json.views.get_group_accordance group.id, g.id %}',
								{% endif %}
								{% ifequal group.type "KLUB" %}
								optName : '{{ g.getPartyName }}',
								{% else %}
								optName : '{{ g.name|truncatewords:4 }}',
								{% endifequal %}
								imgCache : '',
								groupId : {{ g.id }},
								groupSlug : '{{ g.slug }}'
							}
						{% if not forloop.last %}
							,
						{% endif %}
					{% endfor %}
				]
			}
		{% if not forloop.last %}
			,
		{% endif %}
		{% endfor %}
	];

{% if accordGroupType %}
var currentTypeIndex = {{ accordGroupType }};
{% else %}
var currentTypeIndex = 0;
{% endif %}

function groupTypeChanged(elemId)
{
	newTypeIndex = $('#' + elemId)[0].selectedIndex;
	if (newTypeIndex == 0 || currentTypeIndex == newTypeIndex)
		return;


	charts = accordCharts[newTypeIndex].groups;
	$('#accordanceCharts-control').empty();
	for (i in charts) {
		$('#accordanceCharts-control').append(
			'<option value="' + charts[i].groupId + '">' + charts[i].optName + '</option>'
		);
	}
	currentTypeIndex = newTypeIndex;

	return;
}

function groupChanged(elemId)
{
	newIndex = $('#' + elemId)[0].selectedIndex;
	charts = accordCharts[currentTypeIndex].groups;
	divId = 'accordanceCharts';

	if (charts[newIndex].imgCache.charAt(0) == "<") {
		$('#' + divId).html(charts[newIndex].imgCache);
		newUrl = '?accordGroupType=' + newTypeIndex;
		newUrl = newUrl + '&accordGroup=' + accordCharts[newIndex].groupSlug;
		$('#link-me').attr('href', newUrl);
	}
	else {
		$('#' + divId).html('<img src="{{ MEDIA_URL }}/images/wait.gif"/>');
		$.ajax({
				url : charts[newIndex].url,
				async : true,
				dataType : 'json',
				success : function(data) {
						newUrl = '?accordGroupType=' + newTypeIndex;
						newUrl = newUrl + '&accordGroup=' + charts[newIndex].groupSlug;
						$('#link-me').attr('href', newUrl);
						html = "";
						html = '<p><span class="loyal-chart">';
						html += data.loyalChart;
						html += '</span>';
						if (data.partyPie != null) {
							html += '<span class="party-pie">';
							html += data.partyPie;
							html += '</span>';
						}
						html += '</p>';
						$('#' + divId).html(html);
						charts[newIndex].imgCache = html;
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
						$('#' + divId).html('<img src="{{ MEDIA_URL }}/images/error.gif"/>');
				}
		});
	}
}

{% comment %}
Following block should be added into day view only
{% endcomment %}
{% ifequal tabIndex 0 %}
function createGrid(tableId, pagerId, url, caption)
{
	$(tableId).jqGrid({
				url: url,
				datatype: 'json',
				colNames: [	"{% trans "Číslo hlasování" %}",
							"{% trans "Název hlasování" %}",
							"{% trans "Výsledek" %}",
							"{% trans "Loajální" %}"
					],
				colModel :[
					{ name: "pollNo", index: "pollNo", width: 80, jsonmap: "pollNo", sortable: false },
					{ name: "title", index: "title", width: 200, jsonmap: "title", sortable: false },
					{ name: "result", index: "result", width: 120, jsonmap:"result", sortable: false },
					{ name: "loyal", index: "loyal", width: 80, jsonmap: "loyal", sortable: false}
				], 
				pager: $(pagerId),
				jsonReader: {
					root: "rows",
					id: "pk",
					page: "page",
					total: "total",
					records: "records",
					repeatitems: false
				},
				sortname:"start",
				sortorder:"desc",
				rowNum:10,
				rowList:[10,15,20],
				viewrecords:true,
				caption: caption,
				width:640,
				toolbar: [true,'top']
	});
	$(tableId).navGrid(pagerId,{edit:false,add:false,del:false});
}

var filterEnabled = false;

function filterProcedural(id)
{% comment %}
This is callback, that handles checkbox change events
{% endcomment %}
{
	if (filterEnabled) {
		filterEnabled = false;
	}
	else {
		filterEnabled = true;
	}

	grids = [];
	grids[0] = '#aye';
	grids[1] = '#nay';
	grids[2] = '#refrain';
	grids[3] = '#absences';

	{% comment %}
	We are going to retrieve Data URL for each particular grid
	{% endcomment %}
	for (i in grids) {
		$(grids[i]).setGridParam({
			url : getUrl(	$(grids[i]).getGridParam('url'))
		}).trigger("reloadGrid");
	}
}

function getUrl(urlStr)
{% comment %}
it checks if 'Proceduralni hlasovani' polls should be removed from grids,
if so, it then appends an option ?filterProcedural=1 to url.
{% endcomment %}
{
	var retVal = "";

	if (filterEnabled)
		retVal = urlStr + '?filterProcedural=1';
	else
		retVal = urlStr.replace(/\?filterProcedural=1/g,'');

	return retVal
}

{% endifequal %}

{% endblock javascript-head %}

{% block document-ready %}
	
	{{ block.super }}

	for (i in accordCharts) {
		$('#groupType-control').append(
			'<option value="' + i + '">' + accordCharts[i].groupTypeOpt + '</option>'
		);
	}

	if (currentTypeIndex == 0)
		currentTypeIndex += 1;

	charts = accordCharts[currentTypeIndex].groups;
	for (i in charts) {
		$('#accordanceCharts-control').append(
			'<option value="' + charts[i].groupId + '">' + charts[i].optName + '</option>'
		);
	}
	

{% ifequal tabIndex 0 %}
	createGrid(
		"#aye",
		"#aye-pager",
		getUrl("{% url json.views.get_group_aye group.slug date.year date.month date.day %}"),
		"{% trans "Návrhy skupinou podpořené" %}"
	);
	
	createGrid(
		"#nay",
		"#nay-pager",
		getUrl("{% url json.views.get_group_nay group.slug date.year date.month date.day %}"),
		"{% trans "Návrhy skupinou zamítnuté" %}"
	);

	createGrid(
		"#refrain",
		"#refrain-pager",
		getUrl("{% url json.views.get_group_refrains group.slug date.year date.month date.day %}"),
		"{% trans "Hlasování, kdy jsme se zdrželi" %}"
	);

	createGrid(
		"#absences",
		"#absences-pager",
		getUrl("{% url json.views.get_group_absences group.slug date.year date.month date.day %}"),
		"{% trans "Zapečená hlasování, kdy jsme nebyli přítomni" %}"
	);
{% endifequal %}

{% endblock document-ready %}

{% block main-tab %}

<div class="ui-widget">
	<div class="ui-widget-header">
		<h2>{% trans "Shoda hlasování" %}</h2>
	</div>
	<div class="ui-widget-content">
		<div class="left">
			<div class="center" id="accordanceCharts">
				{{ accordanceChart.img|safe|mf:"loyal-chart" }}
				{{ accordPartyPie.img|safe|mf:"party-pie" }}
			</div>
			<div class="center groupSelect">
				<select name="groupType" id="groupType-control" onChange="groupTypeChanged(this.id)">
				</select>
				<select name="accordanceCharts" id="accordanceCharts-control" onChange="groupChanged(this.id)">
				</select>
			</div>
		</div>
		<div class="right">
			<p class="tab-par"> 
				{% with group.name as groupName %}
				{% blocktrans %}
				Nejprve je třeba zvolit skupinu pro níž chceme graf shody se skupinou {{ groupName }} zobrazit.
				{% endblocktrans %}
				{% endwith %}
			</p>
			<p>
			{% if accordGroupType %}
				<a href="?accordGroupType={{ accordGroupType }}&accordGroup={{ accordGroupSlug }}" id="link-me">link</a>
			{% else %}
				<a href="" id="link-me">link</a>
			{% endif %}
			</p>
		</div>
		<div class="clear"></div>
	</div>
</div>

{% ifequal tabIndex 0 %}
<div class "ui-widget">
	<div class="ui-widget-header">
		<h2>{% trans "Jak jsme hlasovali" %}</h2>
	</div>
	<div class="ui-widget-content">
		<p>
		<input type="checkbox" id="filterProcedural" onclick="filterProcedural(this.checked)">
			{% trans "Skryj procedurální hlasování" %}  <br/> 
		</p>

		<table id="aye" class="scroll" cellpadding="0" cellspacing="0">
		</table>
		<div id="aye-pager" class="scroll" style="text-align:center;">
		</div>

		<table id="nay" class="scroll" cellpadding="0" cellspacing="0"> 
		</table>
		<div id="nay-pager" class="scroll" style="text-align:center;">
		</div>

		<table id="refrain" class="scroll" cellpadding="0" cellspacing="0"> 
		</table>
		<div id="refrain-pager" class="scroll" style="text-align:center;">
		</div>

		<table id="absences" class="scroll" cellpadding="0" cellspacing="0"> 
		</table>
		<div id="absences-pager" class="scroll" style="text-align:center;">
		</div>
	</div>
</div>
{% endifequal %}
{% endblock main-tab %}

